<template>
  <div class="wrapper">
    <div style="margin: 50px auto; background-color: white; width: 585px; border-radius: 10px; overflow: hidden">
      <div style="margin: 20px 0; text-align: center; fent-size: 24px;">
        <b>综合服务系统前台</b>
      </div>

      <div style="margin: 50px auto; background-color: white; width: 350px; border-radius: 10px; overflow: hidden">
        <el-tabs type="card" v-model="activeName">
          <el-tab-pane label="账号登录" name="first">
            <el-form :model="user" :rules="rules" ref="userForm">
              <el-form-item prop="username">
                <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-user"
                          v-model="user.username"></el-input>
              </el-form-item>

              <el-form-item prop="password">
                <el-input size="medium"
                          style="margin: 10px 0"
                          prefix-icon="el-icon-lock"
                          show-password
                          v-model="user.password"
                ></el-input>
              </el-form-item>

              <el-form-item style="margin: 10px 0; text-align: right">
                <el-button type="primary" size="small" autocomplete="off" @click="LoginAccount">登录</el-button>
                <el-button type="warning" size="small" autocomplete="off" @click="register">注册</el-button>
              </el-form-item>

              <el-form-item style="margin: 10px 0; text-align: right">
                <el-button type="text" size="mid" autocomplete="off" @click="handlePass">通过邮箱找回密码</el-button>
              </el-form-item>
            </el-form>

          </el-tab-pane>
          <el-tab-pane label="邮箱登录" name="second">
            <el-form :model="user" :rules="rules" ref="emailForm">
              <el-form-item prop="email">
                <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-message"
                          v-model="user.email"></el-input>
              </el-form-item>

              <el-form-item prop="code">
                <el-input size="medium" style="width: 210px" prefix-icon="el-icon-lock" v-model="user.code"></el-input>
                <el-button type="primary" size="small" class="ml-5" @click="sendEmailCode(1)">获取验证码
                </el-button>
              </el-form-item>
              <!--              <el-form-item>-->
              <!--                <div style="display: flex">-->
              <!--                  <el-input size="mid" v-model="code" style="width: 200px"></el-input>-->
              <!--                  <span @click="refreshCode" style="cursor: pointer; flex: 1">-->
              <!--                  <Identify :identifyCode="identifyCode"></Identify>-->
              <!--                </span>-->
              <!--                </div>-->
              <!--              </el-form-item>-->

              <el-form-item style="margin: 10px 0; text-align: right">
                <el-button type="primary" size="small" autocomplete="off" @click="LoginEmail">登录</el-button>
                <el-button type="warning" size="small" autocomplete="off" @click="register">注册</el-button>
              </el-form-item>

              <el-form-item style="margin: 10px 0; text-align: right">
                <el-button type="text" size="mid" autocomplete="off" @click="handlePass">通过邮箱找回密码</el-button>
              </el-form-item>

            </el-form>

          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <el-dialog title="找回密码" :visible.sync="dialogFormVisible" width="40%">
      <el-form label-width="100px" size="small">
        <el-form-item Label="邮箱 ">
          <el-input v-model="pass.email" autocompLete="off"></el-input>
        </el-form-item>
        <el-form-item labeL="验证码">
          <el-input size="small" style="width: 210px" prefix-icon="el-icon-lock" v-model="pass.code"></el-input>
          <el-button type="primary" size="small" class="ml-5" @click="sendEmailCode(2)">获取验证码</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type=" primary" @click="passwordBack">重置密码</el-button>
      </div>
    </eL-diaLog>
  </div>
</template>

<script>
import {setRoutes} from "@/router";

export default {
  name: "Login",
  data() {
    return {
      user: {},
      activeName: 'first',
      code: '',
      dialogFormVisible: false,
      // 图片验证码
      identifyCode: '',
      pass: {},
      identifyCodes: '3456789ABCDEFGHGKHNPORSTUVIXY',
      rules: {
        username: [
          {required: true, message: "请输入用户名", trigger: "blur"},
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符以内",
            trigger: "blur",
          },
        ],
        password: [
          {required: true, message: "请输入密码", trigger: "blur"},
          {
            min: 1,
            max: 20,
            message: "长度在 1 到 20 个字符以内",
            trigger: "blur",
          },
        ],
      },
    };
  },
  // components:{
  //   Identify
  // },
  mounted() {
    this.refreshCode()
  },
  methods: {
    register() {
      this.$router.push('/frontRegister')
      setTimeout(() => {
        location.reload();
      }, 100);
    },
    sendEmailCode(type) {
      console.log(this.pass.email)
      let email;
      if (type === 1) {
        email = this.user.email

      } else if (type === 2) {
        email = this.pass.email;
      }
      if (!email) {
        this.$message.warning("请输入邮箱账号")
        return;
      }
      if (!/^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/.test(email)) {
        this.$message.warning("请输入正确邮箱账号")
        return;
      }

      // 发送邮箱验证码
      this.request.get("/user/email/" + email + "/" + type).then((res) => {
        if (res.code == '200') {
          this.$message.success("发送成功")
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    LoginAccount() {
      // if (this.code !== this.identifyCode.toLowerCase()) {
      //   this.$message({
      //     type: "error",
      //     message: "验证码错误"
      //   })
      //   return;
      // }
      this.$refs["userForm"].validate((valid) => {
        if (valid) {
          this.request.post("/user/loginAccount", this.user).then((res) => {
            if (res.code == "200") {
              localStorage.setItem("user", JSON.stringify(res.data)) //储存用户信息到浏览器
              localStorage.setItem("menus", JSON.stringify(res.data.menus)) //储存用户信息到浏览器
              // 动态设置当前用户的路由
              setRoutes()
              this.$router.push("/front/home")

              setTimeout(() => {
                this.$message.success("登录成功")
              }, 500)

              // if(res.data.role === 'ROLE_USER'){
              // }else {
              //   this.$router.push("/");
              // }
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
    LoginEmail() {
      // if (this.code !== this.identifyCode.toLowerCase()) {
      //   this.$message({
      //     type: "error",
      //     message: "验证码错误"
      //   })
      //   return;
      // }
      if (!this.user.email) {
        this.$message.warning("请输入邮箱")
        return;
      }
      if (!this.user.code) {
        this.$message.warning("请输入验证码")
        return;
      }
      this.$refs["emailForm"].validate((valid) => {
        if (valid) {
          this.request.post("/user/loginEmail", this.user).then((res) => {
            if (res.code == "200") {
              localStorage.setItem("user", JSON.stringify(res.data)) //储存用户信息到浏览器
              localStorage.setItem("menus", JSON.stringify(res.data.menus)) //储存用户信息到浏览器
              // 动态设置当前用户的路由
              setRoutes()
              this.$router.push("/front/home")
              this.$message.success("登录成功")
              setTimeout(() => {
                location.reload();
              }, 500)
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
    // 切换验证码
    refreshCode() {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4);
    },
    // 随机生成验证码
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[Math.floor(Math.random() * (this.identifyCodes.length))]
      }
    },
    handlePass() {
      this.dialogFormVisible = true
      this.pass = {}
    },
    passwordBack() {
      this.request.put("/user/reset", this.pass).then(res => {
        if (res.code === '200') {
          this.$message.success("重置密码成功，新密码为: 123， 请尽快修改密码")
          this.dialogFormVisible = false
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  }
}
</script>

<style scope>
.wrapper {
  height: 100vh;
  /*background-image: linear-gradient(to bottom right, #fc466b, #3f5efb);*/
  background-size: 100%;
  background-image: url("/src/assets/R-C2.jpg");
  overflow: hidden;
}
</style>